//
//  RefuseOrderCell.m
//  ios-supplier
//
//  Created by feynmanyuan on 15/4/17.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//  绘制拒单的cell
//

#import "RefuseOrderCell.h"
#import "JFBRefuseOrderDS.h"
#import "JFBPhoneCallHelper.h"

@interface RefuseOrderCell(){
    UILabel *_roleLabel;
    UILabel *_reasonLabel;
    UILabel *_paymentLabel;
    
    UILabel *_refundStatusLabel;
    UILabel *_moneyLabel;
//    UILabel *_requireTimeLabel;
    
    UILabel *_userNameLabel;
    UILabel *_addressLabel;
    UILabel *_staffLabel;
    
    UIView *_spacingView;
    
    NSUInteger line1;
//    NSUInteger line2;
    NSUInteger line3;
    
    UIButton *_call;
    JFBPhoneCallHelper *_callHelper;
    
    UIView *_line1View;
}
@end

@implementation RefuseOrderCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier frame:(CGRect)frame{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier frame:frame];
    if (self) {
        [self initSubView];
    }
    return self;
}

-(void)initSubView{
    _spacingView = [[UIView alloc] init];
    [_spacingView setBackgroundColor:PanelBgColorDefault];
    [self addSubview:_spacingView];
    
    _roleLabel = [[UILabel alloc] init];
    _roleLabel.font = [UIFont systemFontOfSize:TextFontSizeH2];
    _roleLabel.textColor = TextColorImportant;
    [self addSubview:_roleLabel];
    
    _reasonLabel = [[UILabel alloc] init];
    _reasonLabel.font = [UIFont systemFontOfSize:TextFontSizeH3];
    _reasonLabel.textColor = TextColorDefault;
    [self addSubview:_reasonLabel];
    
    _paymentLabel = [[UILabel alloc]init];
    _paymentLabel.font = [UIFont systemFontOfSize:TextFontSizeH3];
    _paymentLabel.textColor = TextColorLight;
    [self addSubview:_paymentLabel];
    
    _refundStatusLabel = [[UILabel alloc] init];
    _refundStatusLabel.font = [UIFont systemFontOfSize:TextFontSizeH3];
    _refundStatusLabel.textColor = TextColorLight;
    [self addSubview:_refundStatusLabel];
    
    _moneyLabel = [[UILabel alloc] init];
    _moneyLabel.font = [UIFont systemFontOfSize:TextFontSizeH3];
    _moneyLabel.textColor = TextColorDefault;
    [self addSubview:_moneyLabel];
    
//    _requireTimeLabel = [[UILabel alloc] init];
//    _requireTimeLabel.font = [UIFont systemFontOfSize:TextFontSizeH3];
//    _requireTimeLabel.textColor = TextColorImportant;
//    [self addSubview:_requireTimeLabel];
    
    _userNameLabel = [[UILabel alloc] init];
    _userNameLabel.font = [UIFont systemFontOfSize:TextFontSizeH3];
    _userNameLabel.textColor = TextColorDefault;
    [self addSubview:_userNameLabel];
    
    _addressLabel = [[UILabel alloc] init];
    _addressLabel.font = [UIFont systemFontOfSize:TextFontSizeH3];
    _addressLabel.textColor = TextColorDefault;
    _addressLabel.lineBreakMode = NSLineBreakByCharWrapping;
    _addressLabel.numberOfLines = 0;
    [self addSubview:_addressLabel];
    
    _line1View = [[UIView alloc] init];
    _line1View.backgroundColor = LineColorImportant ;
    [self addSubview: _line1View];
//    _staffLabel = [[UILabel alloc] init];
//    _staffLabel.font = [UIFont systemFontOfSize:TextFontSizeH3];
//    _staffLabel.textColor = TextColorDefault;
//    [self addSubview:_staffLabel];
}

-(void)setOrder:(JFBOrderDS *)order1{
    JFBRefuseOrderDS *order = (JFBRefuseOrderDS*)order1;
    
    NSString *role = [NSString stringWithFormat:@"【%@】", order.role];
    CGSize roleSize = [role sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH2]}];
    CGFloat roleX = kOrderCellMargin, roleY = (kOrderCellHeaderHeight - roleSize.height) / 2;
    CGRect roleRect = CGRectMake(roleX, roleY, roleSize.width, roleSize.height);
    _roleLabel.text = role;
    _roleLabel.frame = roleRect;
    
    if ([order.reason isEqual: [NSNull null]]) {
        order.reason = @"";
    }
    CGSize reasonSize = [order.reason sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
    CGFloat reasonX = roleX + roleSize.width + kOrderCellSpacing / 4, reasonY = roleY;
    CGRect reasonRect = CGRectMake(reasonX, reasonY, reasonSize.width, reasonSize.height);
    _reasonLabel.text = order.reason;
    _reasonLabel.frame = reasonRect;
    
    NSString *money = [NSString stringWithFormat:@"¥ %@", [NSString decimalToString:order.money]];
    CGSize moneySize = [money sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
    CGFloat moneyY = roleY;
    CGFloat moneyX = self.bounds.size.width - kOrderCellSpacing - moneySize.width;
    CGRect moneyRect = CGRectMake(moneyX, moneyY, moneySize.width, moneySize.height);
    _moneyLabel.text = money;
    _moneyLabel.frame = moneyRect;
    
    CGSize paymentSize = [order.payment sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
    CGFloat paymentY = roleY;
    CGFloat paymentX = self.bounds.size.width - kOrderCellSpacing * 2 - moneySize.width - paymentSize.width;
    CGRect paymentRect = CGRectMake(paymentX, paymentY, paymentSize.width, paymentSize.height);
    _paymentLabel.text = order.payment;
    _paymentLabel.frame = paymentRect;

    
    CGSize refundStatusSize = [order.refundStatus sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
    CGFloat refundStatusY = roleY;
    CGFloat refundStatusX = moneyX - refundStatusSize.width - kOrderCellSpacing;
    CGRect refundStatusRect = CGRectMake(refundStatusX, refundStatusY, refundStatusSize.width, refundStatusSize.height);
    _refundStatusLabel.text = order.refundStatus;
    _refundStatusLabel.frame = refundStatusRect;
    
    line1 = kOrderCellHeaderHeight;
    
//    [self.drawLineHelper addHorizontalLine:0 y:line1 color:LineColorImportant width:self.bounds.size.width];
    _line1View.frame = CGRectMake(0, line1, self.bounds.size.width, 1);
//
//    CGSize requireTimeSize = [order.createAt sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
//    CGFloat requireTimeY = kOrderCellHeaderHeight + kOrderCellSpacing;
//    CGFloat requireTimeX = (self.bounds.size.width - requireTimeSize.width) / 2;
//    CGRect requireTimeRect = CGRectMake(requireTimeX, requireTimeY, requireTimeSize.width, requireTimeSize.height);
//    _requireTimeLabel.text = order.createAt;
//    _requireTimeLabel.frame = requireTimeRect;
    
//    line2 = requireTimeY + requireTimeSize.height + kOrderCellSpacing;
    
    NSString *userName = [NSString stringWithFormat:@"%@  %@", order.userName, order.userMobile];
    CGSize userNameSize = [userName sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
    CGFloat userNameY = kOrderCellHeaderHeight + kOrderCellSpacing;
    CGFloat userNameX = kOrderCellMargin;
    CGRect userNameRect = CGRectMake(userNameX, userNameY, userNameSize.width, userNameSize.height);
    _userNameLabel.text = userName;
    _userNameLabel.frame = userNameRect;
    
    NSString * address = [order.address isEqual:[NSNull null]] ? @"" : order.address;
//    CGSize addressSize = [address sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
    CGFloat addressY = userNameY + userNameSize.height + kOrderCellSpacing;
    CGFloat addressX = kOrderCellMargin;
    _addressLabel.text = address;
    CGSize addressSize = [_addressLabel sizeThatFits:CGSizeMake(self.bounds.size.width - 98 - kOrderCellSpacing * 2, MAXFLOAT)];
    CGRect addressRect = CGRectMake(addressX, addressY, addressSize.width, addressSize.height);
    _addressLabel.frame = addressRect;
    
    
    
    _callHelper = [[JFBPhoneCallHelper alloc] initWithMobile:order.userMobile color:TextColorImportant];
    _call = [_callHelper getButton];
    [self addSubview:_call];
    CGRect callRect = CGRectMake(self.bounds.size.width - kOrderCellSpacing - 88, userNameRect.origin.y, 88, 36);
    _call.frame = callRect;
    
//    line3 = addressY + addressSize.height + kOrderCellSpacing;
    
//    NSString *staff = [NSString stringWithFormat:@"配送人员：%@(%@)", order.staff, order.staffMobile];
//    CGSize staffSize = [staff sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
//    CGFloat staffX = kOrderCellMargin, staffY = line3 + kOrderCellSpacing;
//    CGRect staffRect = CGRectMake(staffX, staffY, staffSize.width, staffSize.height);
//    _staffLabel.text = staff;
//    _staffLabel.frame = staffRect;
    
    _spacingView.frame = CGRectMake(0, addressY + addressSize.height + kOrderCellSpacing, self.bounds.size.width, kOrderCellSpacing);
    
    self.height = CGRectGetMaxY(_spacingView.frame);
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
